<?php
namespace Admin\Controller;
use Think\Controller;
class GoodsController extends Controller {
    public function showlist()
    {   
        $data =  D('Goods')->search();
         //var_dump($list);die;
        $this->assign($data);
        $this->display();
    }
    public function category()
    {
        // $btn = makeAlipayBtn(1);
        // echo $btn;die;
        //$this->assign('btn',$btn);
        $good_cates = D('Goods')->cate();
        $this->assign('good_cates',$good_cates);
        $this->display();
    }
    public function category_add()
    {   
        if(IS_POST){
            $data = I('post.');
            $config = array(
                // 'maxSize'   =>  3145728,
                'rootPath'  =>  './Public/',
                'savePath'  =>  'Uploads/',
                // 'viewPath'  =>  'Uploads/',
                'exts'      =>  array('jpg','png','jpeg'),
            );
            $upload = new \Think\Upload($config);
            $info = $upload->upload();
            if(!$info){
                $this->error($upload->getError());
            }else{
                $data['img'] = 'Public/'.$info['img']['savepath'].$info['img']['savename'];
            }
            $data['id'] = D('Good_cates')->max('id')+1;
            $z = D('Good_cates')->add($data);
            if($z){
                $this->success('添加分类成功',U('category_add'));
            }
        }else{
            $category = D('Goods')->getTreeList();
            foreach($category as $k=>$v){
                if($v['deep'] == 2){
                    unset($category[$k]);
                }
            }

            $this->assign('cat_list',$category);
            $this->display(); 
        }
        
    }
    public function product_start()
    {
        $id = I('get.id');
        $z = D('Goods')->where('id='.$id)->setField('is_sale','0');
        if($z){
            echo 'ok';
        }
    }
    public function product_stop()
    {
        $id = I('get.id');
        $z = D('Goods')->where('id='.$id)->setField('is_sale','1');
        if($z){
            echo 'ok';
        }
    }
    public function product_del()
    {
        $id = I('get.id');
        $z = D('Goods')->where('id='.$id)->setField('is_delete','1');
        if($z){
            echo 'ok';
        }
    }
    public function product_add()
    {   
        if(IS_POST)
        {
            $data = I('post.');
//            if(empty($data['product_name']) || empty($data['product_name'])){
//
//            }
            $config = array(
                // 'maxSize'   =>  3145728,
                'rootPath'  =>  './Public/',
                'savePath'  =>  'Uploads/Goods/',
                // 'viewPath'  =>  'Uploads/',
                'exts'      =>  array('jpg','png','jpeg'),
            );
            $upload = new \Think\Upload($config);
            $info = $upload->upload();
            if(!$info){
                $this->error($upload->getError());
            }else{
                $data['img1'] = 'Public/'.$info['img1']['savepath'].$info['img1']['savename'];
                $data['img2'] = 'Public/'.$info['img2']['savepath'].$info['img2']['savename'];
                $data['img3'] = 'Public/'.$info['img3']['savepath'].$info['img3']['savename'];
            }

            $id = D('Goods')->max('id');
            $data['id'] = $id+1;
            $data['cn'] = "WLGP".str_pad($id+1,6,"0",STR_PAD_LEFT);
            $z = D('Goods')->add($data);
            if($z){
                $this->success('添加商品成功',U('Goods/showlist'));
            }else{
                $this->error('添加失败，请重新添加',U('Goods/showlist'));
            }
        }else{
            $list = D('Goods')->getTreeList();
            $this->assign('cat_list',$list);
            $brand = D('Good_brand')->select();
            $this->assign('brand',$brand);
            $region = D('Good_region')->select();
            $this->assign('region',$region);
            $this->display();  
        }
    }
    public function product_edit()
    {
        if(IS_POST)
        {
            $data = I('post.');
            $flag = empty($_FILES['img1']['tmp_name']) && empty($_FILES['img2'][tmp_name]) && empty($_FILES['img3'][tmp_name]);
            if(!$flag){
                $config = array(
                    // 'maxSize'   =>  3145728,
                    'rootPath'  =>  './Public/',
                    'savePath'  =>  'Uploads/Goods/',
                    // 'viewPath'  =>  'Uploads/',
                    'exts'      =>  array('jpg','png','jpeg'),
                );
                $upload = new \Think\Upload($config);
                $info = $upload->upload();
                if(!$info){
                    $this->error($upload->getError());
                }else{
                    $data['img1'] = 'Public/'.$info['img1']['savepath'].$info['img1']['savename'];
                    $data['img2'] = 'Public/'.$info['img2']['savepath'].$info['img2']['savename'];
                    $data['img3'] = 'Public/'.$info['img3']['savepath'].$info['img3']['savename'];
                }
            }

            $z = D('Goods')->save($data);
            if($z){
                $this->success('修改商品成功',U('Goods/showlist'));
            }else{
                $this->error('修改失败，请重新修改',U('Goods/showlist'));
            }
        }else{
            $brand = D('Good_brand')->select();
            $this->assign('brand',$brand);
            $region = D('Good_region')->select();
            $this->assign('region',$region);

            $id = $_GET['id'];
            $data = D('Goods')->find($id);
            $list = D('Goods')->getTreeList();
            $this->assign('cat_list',$list);
            $this->assign('good',$data);
            $this->display();
        }
    }
    public function region()
    {
        // $btn = makeAlipayBtn(1);
        // echo $btn;die;
        //$this->assign('btn',$btn);
        $region = D('Goods')->region();
        $this->assign('region',$region);
        $this->display();
    }
    public function region_add()
    {
        if(IS_POST){
            $data = I('post.');
            $config = array(
                // 'maxSize'   =>  3145728,
                'rootPath'  =>  './Public/',
                'savePath'  =>  'Uploads/',
                // 'viewPath'  =>  'Uploads/',
                'exts'      =>  array('jpg','png','jpeg'),
            );
            $upload = new \Think\Upload($config);
            $info = $upload->upload();
            if(!$info){
                $this->error($upload->getError());
            }else{
                $data['img'] = 'Public/'.$info['img']['savepath'].$info['img']['savename'];
            }
            $data['id'] = D('Good_region')->max('id')+1;
            $z = D('Good_region')->add($data);
            if($z){
                $this->success('添加分类成功',U('region_add'));
            }
        }else{
            $this->display();
        }

    }
    public function brand()
    {
        // $btn = makeAlipayBtn(1);
        // echo $btn;die;
        //$this->assign('btn',$btn);
        $brand = D('Goods')->brand();
        $this->assign('brand',$brand);
        $this->display();
    }
    public function brand_add()
    {
        if(IS_POST){
            $data = I('post.');
            $config = array(
                // 'maxSize'   =>  3145728,
                'rootPath'  =>  './Public/',
                'savePath'  =>  'Uploads/',
                // 'viewPath'  =>  'Uploads/',
                'exts'      =>  array('jpg','png','jpeg'),
            );
            $upload = new \Think\Upload($config);
            $info = $upload->upload();
            if(!$info){
                $this->error($upload->getError());
            }else{
                $data['img'] = 'Public/'.$info['img']['savepath'].$info['img']['savename'];
            }
            $data['id'] = D('Good_brand')->max('id')+1;
            $z = D('Good_brand')->add($data);
            if($z){
                $this->success('添加分类成功',U('brand_add'));
            }
        }else{
            $this->display();
        }

    }


}